import * as fse from 'fs-extra';
import path from 'path';

import { runHvigorAsync } from '../../common/common';
import {
  AbilityType,
  APIType,
  APIVersion, CaseLevel,
  LanguageType, ModuleTemplateType,
  RuntimeOSType,
  SDKType
} from '../../plugins/project-builder/models/constants';
import { TestCaseBuilder } from '../../plugins/task-builder/task-case-builder';
import { ProjectBuildOption, TestCaseFun } from '../../plugins/task-builder/test-case';
import { TestTaskOptionBuilder } from '../../plugins/task-builder/test-task-option';


const projectBuilderOption: ProjectBuildOption = {
  template: {
    sdk: SDKType.Single,
    runtimeOS: RuntimeOSType.HarmonyOS,

    // 当前都是5.0 api已经不用管
    apiVersion: APIVersion.API11,
    apiType: APIType.STAGE,
    language: LanguageType.ArkTs,
    abilityType: AbilityType.Application,
    entryTemplateType: ModuleTemplateType.EmptyAbility
  },
  modulesOption: [ModuleTemplateType.StaticLibrary]
};

// 判断BuildProfile的目录位置是否正确， hap/hsp在build目录下， har在根目录下
const testfun: TestCaseFun = async (project, taskConfig, record) => {
  // 1. 创建API12 stage 工程 ，添加 StaticLibrary 模块， 获取模块路径
  const entryPath = project.getModule('entry').moduleRootPath();
  const staticLibraryPath = project.getModule('staticLibrary').moduleRootPath();

  // 2、构建并识别BuildProfile.ets文件的目录位置
  const commands: string[] = 'assembleHap assembleHar -p buildMode=release'.split(' ');
  const result = await runHvigorAsync(project.projectRootPath(), commands);
  record.addStep('初始构建hap hsp', result);
  expect(result.stdout).toContain('BUILD SUCCESSFUL');

  // 3、 判断BuildProfile的目录位置是否正确， hap/hsp在build目录下， har在根目录下
  const entryBuildPath = path.resolve(entryPath, 'build', 'default', 'generated',
    'profile', 'default', 'BuildProfile.ets');
  const entryRootPath = path.resolve(entryPath, 'BuildProfile.ets');
  const harBuildPath = path.resolve(staticLibraryPath, 'build', 'default', 'generated',
    'profile', 'default', 'BuildProfile.ets');
  const harRootPath = path.resolve(staticLibraryPath, 'BuildProfile.ets');
  expect(fse.existsSync(entryBuildPath)).toEqual(true);
  expect(fse.existsSync(entryRootPath)).toEqual(false);
  expect(fse.existsSync(harBuildPath)).toEqual(false);
  expect(fse.existsSync(harRootPath)).toEqual(true);
};

TestCaseBuilder.builder()
  .caseName('DTS2024060528186')
  .description('buildProfile生成位置不在build里，在工程目录里面乱放')
  .projectBuilderOption(projectBuilderOption)
  .taskConfig(TestTaskOptionBuilder.builder().caseLevel(CaseLevel.DTS).build())
  .testFun(testfun)
  .load();